package servlet;

import bean.Movie;
import dao.impl.MovieDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

@WebServlet("/admin/add_movie")
@MultipartConfig
public class AdminAddMovie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8"); // 设置请求编码为UTF-8
        response.setContentType("text/html;charset=UTF-8"); // 设置响应内容类型和编码
            String name = request.getParameter("name");
            String totalTime = request.getParameter("totalTime");
            String mainActor = request.getParameter("mainActor");
            Part part = request.getPart("img");
            String imgName = part.getSubmittedFileName();
            String price = request.getParameter("price");
            String type = request.getParameter("type");
            String date = request.getParameter("date");
            String detail = request.getParameter("detail");
            Movie movie = new Movie();
            movie.setPrice(new BigDecimal(price));
            movie.setDate(date);
            movie.setTotalTime(new Long(totalTime));
            movie.setType(type);
            movie.setImg(imgName);
            movie.setName(name);
            movie.setMain_actor(mainActor);
            movie.setdetail(detail);
            boolean success =  new MovieDaoImpl().addMovie(movie);
            if(success){
                String path ="C:\\Users\\Administrator\\Downloads\\cenima-buy-ticket-whw\\out\\artifacts\\cenima_buy_ticket_Web_exploded\\img\\movie\\";
                String webPath = path + imgName;
                File fileOnWeb = new File(webPath);
                if (fileOnWeb.exists()) {
                    fileOnWeb.delete();
                }
                part.write(webPath);
                String localPath = "C:\\Users\\Administrator\\Downloads\\cenima-buy-ticket-whw\\web\\img\\movie\\" + imgName;
                File fileOnLocal = new File(localPath);
                if (fileOnLocal.exists()) {
                    fileOnLocal.delete();
                }
                Files.copy(fileOnWeb.toPath(), fileOnLocal.toPath(), StandardCopyOption.REPLACE_EXISTING); // 使用Java NIO复制文件
                request.setAttribute("message", "添加成功");
                request.setAttribute("page", "aindex.jsp");
                request.getRequestDispatcher("alert.jsp").forward(request, response);
            }
    }
}
